package org.op.editor;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.op.menu.MenuActionController;
import org.op.util.MessageController;

public class EditorPopupMenuViewImpl implements PopupMenuView, ActionListener {

	private JPopupMenu menu;
	private MessageController messageController;
	private MenuActionController menuActionController;
	
	public EditorPopupMenuViewImpl() {

	}
	
	public void initMenu() {
		menu = new JPopupMenu();
		addMenuItem(menu, "edit.cut");
		addMenuItem(menu, "edit.copy");
		addMenuItem(menu, "edit.paste");
		
		menu.addSeparator();
		addMenuItem(menu,"save.wikiword");
		
	}
	
	private void addMenuItem(JPopupMenu menu, String name) {
		String label = messageController.getLabel(name);
		
        JMenuItem menuitem = new JMenuItem(label);
        menuitem.addActionListener(this);
        menuitem.setName(name);
        menu.add(menuitem);		
	}
	
	public void showMenu(Component component, Point point) {
		if (menu==null) {
			initMenu();
		}
        menu.show(component, point.x, point.y);
	}

	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}

	public void actionPerformed(ActionEvent event) {
		String menuItemName = ((JMenuItem)event.getSource()).getName();
		menuActionController.doAction(menuItemName);
	}

	public MenuActionController getMenuActionController() {
		return menuActionController;
	}

	public void setMenuActionController(MenuActionController menuActionController) {
		this.menuActionController = menuActionController;
	}
	
	

}
